/**
 * Created by qiank_000 on 2016/10/18.
 */
/// <reference path="jquery.d.ts" />
$(function(){
    /*nav导航*/

    /*video*/
    (function(){
        var $nav = $('#nav');
        var $video = $('#video');
        var $videoWrap = $video.find('.video-wrap');
        var video = $videoWrap.find('video');
        var $close = $video.find('.close2');
        var $txt = $('#video .hudong');

        var flag1 = false;
        var flag2 = true;

        //初始化
        var T = -$nav.outerHeight();
        var L = ( $video.width() - $videoWrap.width() ) / 2;
        $videoWrap.css({"top":T + 'px',"left":L + 'px'});
        $(window).resize(function(){
            var T = -$nav.outerHeight();
            var L = ( $video.width() - $videoWrap.width() ) / 2;
            $videoWrap.css({"top":T + 'px',"left":L + 'px'});
        });
        $videoWrap.css({'display':'none'});

        $txt.click(function(){
            /*flag1 = !flag1;
            flag2 = !flag2;
            flag1 ? $videoWrap.css({'display':'block'}) : $videoWrap.css({'display':'none'});
            flag1 ? video.trigger('play') : video.trigger('pause'); */
            
            $videoWrap.css({'display':'block'});

            

            video.get(0).currentTime = 0;
            video.trigger('play');
            if($(window).width() <= 768){
                /*if(video.get(0).webkitEnterFullscreen){
                    video.get(0).webkitEnterFullscreen();
                }else if(video.get(0).mozRequestFullscreen){
                    video.get(0).mozRequestFullscreen();
                }else if(video.get(0).requestFullscreen){
                    video.get(0).requestFullscreen();
                }*/
                var vt = ( $(window).height() - $video.height() ) / 2;
                video.css({"top":vt + 'px'});
                $close.css({"top":vt + 'px'});
            }
            
            return false;
            
        });
        video.click(function(event){
            flag2 = !flag2;
            flag2 ? video.trigger('play') : video.trigger('pause');
            return false;
            //event.stopPropagation();
        });

        $close.click(function(event){
            $videoWrap.css({'display':'none'});
            video.trigger('pause');
            video.get(0).currentTime = 0;
            flag2 = !flag2;
            console.log('close');
            return false;
            //event.stopPropagation();
        });

        var t = $nav.outerHeight();
        var l = ( $video.width() - $txt.width() ) / 2;
        $txt.css({"top":t + 'px',"left":l + 'px'});
        $(window).resize(function(){
            var t = $nav.outerHeight();
            var l = ( $video.width() - $txt.width() ) / 2;
            $txt.css({"top":t + 'px',"left":l + 'px'});
        });

        //互动
        $(window).load(function(){
            var $width = $('body').width();
            var t = ($width < 768) ? 130 : ($width < 1000) ? 40 : 350;
            $txt.animate({
                'opacity': 1,
                'top': t
            },'slow');
        });
        $(window).resize(function(){
            var $width = $('body').width();
            var t = ($width < 768) ? 130 : ($width < 1000) ? 40 : 350;
            $txt.animate({
                'opacity': 1,
                'top': t
            },'slow');
        });
        var $btn = $('#nav button');
        var btnflag = false;
        $btn.click(function(){
            btnflag = !btnflag;
            if(btnflag){
                $txt.css({'display':'none'});
            }else{
                $txt.css({'display':'block'});
            }
        });
        

    })();

    /*轮播图*/
    (function(){
        var $myCarousel = $('#myCarousel');
        var $prev = $('#prev img');
        var $next = $('#next img');

        //初始化
        var T = ($myCarousel.height() - $prev.height()) / 2;
        $myCarousel.find('a img').css({"margin-top":T + 'px'});
        $(window).resize(function(){
            var T = ($myCarousel.height() - $prev.height()) / 2;
            $myCarousel.find('a img').css({"margin-top":T + 'px'});
        });

        //上一个 下一个图片替换
        $prev.hover(function(){
            $(this).css({"src":"img/left_press.png"});
        },function(){
            $(this).css({"src":"img/left_nopress.png"});
        });
        $next.hover(function(){
            $(this).css({"src":"img/right_press.png"});
        },function(){
            $(this).css({"src":"img/right_nopress.png"});
        });
    })();

    /*form  email*/
    (function(){
        var $inputText = $('form input,form textarea');
        var $btn = $('form button');
        var $loadingImg = $btn.find('img');
        var $span = $('form span');

        //初始化
        $loadingImg.css({"display":"none"});
        var L = ($btn.width() - $loadingImg.width()) / 2;
         $loadingImg.css({"left":L + 'px'});
         $(window).resize(function(){
             var L = ($btn.width() - $loadingImg.width()) / 2;
             $loadingImg.css({"left":L + 'px'});
         });

        $span.css({"visibility":"hidden"});

        $inputText.focus(function(){
            $(this).css({
                "border-color":"#fa5c58",
                "background-color":"#ffffff",
                "box-shadow":"0px 0px 0px #fa5c58"
            });
        });
        $inputText.blur(function(){
            $(this).css({
                "border-color":"#f3f3f3",
                "background-color":"#f3f3f3",
                //"box-shadow":"0px 0px 0px #f3f3f3"
            });
        });



        var $contact = $('form input');
        var $content = $('form textarea');
        $btn.click(function(){
            //ajax访问php
            var contact = $contact.val();
            var content = $content.val();
            
            if(content == ''){
                $inputText.eq(1).css({
                    "border-color":"#fa5c58",
                    "background-color":"#ffffff",
                    "box-shadow":"0px 0px 0px #fa5c58"
                });
                $btn.css({
                    "background-color":"#fa5c58",
                        "color":"#fff"
                    });
                $loadingImg.css({"display":"none"});
                return ;
            }else{
                $btn.css({
                    "background-color":"#b54542",
                    "color":"#b54542"
                });
                $loadingImg.css({"display":"block"});

                //接收表单的URL地址
                var url = "./send.email.php";
                //需要POST的值，把每个变量都通过&来联接
                var postStr  = "contact="+ contact +"&content="+ content;
                //实例化Ajax
                //var ajax = InitAjax();
                var ajax = false;
                //开始初始化XMLHttpRequest对象
                if(window.XMLHttpRequest) { //Mozilla 浏览器
                    ajax = new XMLHttpRequest();
                    if (ajax.overrideMimeType) {//设置MiME类别
                        ajax.overrideMimeType("text/xml");
                    }
                }
                else if (window.ActiveXObject) { // IE浏览器
                    try {
                        ajax = new ActiveXObject("Msxml2.XMLHTTP");
                    } catch (e) {
                        try {
                            ajax = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e) {}
                    }
                }
                if (!ajax) { // 异常，创建对象实例失败
                    window.alert("不能创建XMLHttpRequest对象实例.");
                    return false;
                }
                //通过Post方式打开连接
                ajax.open("POST", url, true);
                //定义传输的文件HTTP头信息
                ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                //发送POST数据
                ajax.send(postStr);
                //获取执行状态
                ajax.onreadystatechange = function() {

                    $btn.css({
                    "background-color":"#fa5c58",
                        "color":"#fff"
                    });
                    $loadingImg.css({"display":"none"});

                    //如果执行状态成功，那么就把返回信息写到指定的层里
                    if (ajax.readyState == 4 && ajax.status == 200) {
                        console.log(ajax.responseText);
                        $span.css({"visibility":"visible"});
                        if(ajax.responseText === "1"){
                            $span.text("提示：我们已经收到信息，感谢您的配合！").css({"color":"#00ff00"});
                            $contact.val('');
                            $content.val('');
                        }else{
                            $span.text("提示：很抱歉，信息发送失败。请尝试").css({"color":"#ff0000"});
                            $('<a href="javascript:;">重新发送！</a>').css({
                                "color":"#00ff00",
                                "text-decoration":"none",
                                "cursor":"pointer"
                            }).appendTo($span);
                        }
                    }
                }
            }
        });


    })();

});